unit uForm_Login;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,
  DrcpSvc,

  uPluginIntf;

type
  TCheckPasswordFunc=function(AUsername:string;APassword:String):boolean of object;

  TForm_Login = class(TForm)
    Label1: TLabel;
    Edit_Username: TEdit;
    Label2: TLabel;
    Edit_Password: TEdit;
    Button_OK: TButton;
    Button_Cancel: TButton;
    procedure Button_OKClick(Sender: TObject);
    procedure Edit_PasswordKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Edit_UsernameKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
  private
    FOnCheckPassword : TCheckPasswordFunc;

    { Private declarations }
    function GetUsername : string;
    procedure SetOnCheckPassword(AFunc : TCheckPasswordFunc);
  public

    { Public declarations }
    property Username : string read GetUsername;
    property OnCheckPassword : TCheckPasswordFunc write SetOnCheckPassword;
  end;

  {
var
  Form_Login: TForm_Login;
   }
implementation

{$R *.dfm}

procedure TForm_Login.Button_OKClick(Sender: TObject);
var
  lLoginRslt : boolean;
begin
  if Edit_Username.Text = '' then
  begin
    Edit_Username.Text := 'type username here';
    Edit_Username.SelectAll;
    Edit_Username.SetFocus;

    Exit;
  end;

  if Assigned(FOnCheckPassword) then
    lLoginRslt := FOnCheckPassword(Edit_Username.Text,Edit_Password.Text)
  else
    Raise Exception.Create('error: onCheckPassword not assigned.');

  if lLoginRslt=false then  //fail
  begin
    ShowMessage('Login fail, try again!');
    //FSoap.Log_Add('SYSTEM',FHostName,'FCS client',0,'Admin','LoginFail','UserId='+Edit_Username.Text);

    Exit;
  end;

  ModalResult := mrOK;
end;

function TForm_Login.GetUsername: string;
begin
  Result := Edit_Username.Text;
end;

procedure TForm_Login.Edit_PasswordKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = vk_return then
    Button_OKClick(self);
end;

procedure TForm_Login.Edit_UsernameKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = vk_return then
    Edit_Password.SetFocus;
end;

procedure TForm_Login.FormShow(Sender: TObject);
begin
  Button_OK.SetFocus;
end;

procedure TForm_Login.SetOnCheckPassword(AFunc: TCheckPasswordFunc);
begin
  FOnCheckPassword := AFunc;
end;

end.
